import axios from 'axios'
import {Message} from 'element-ui'
import router from '../router'


axios.interceptors.request.use(config=>{
  return config;
}, err=> {
  Message.error({message: '请求超时!',showClose: true});
})


axios.interceptors.response.use(success=>{
    if (success.status && success.status == 200 && success.data.status == 500) {
      Message.error({
        message: success.data.message+' '+success.data.object,
        showClose: true
      });
      return;
    }
    if (success.data.message) {
      Message.success({
        message: success.data.message,
        showClose: true
      });
    }
    return success.data;
  },err=> {
    if (err.response.status == 504 || err.response.status == 404 ||err.response.status == undefined||err.response == undefined) {
      Message.error({message: '服务器连接失败⊙﹏⊙∥',showClose: true});
      //Message.error({message:err.response.data.message,showClose: true});
    } else if (err.response.status == 403) {
      Message.error({message: '权限不足,请联系管理员!',showClose: true});
    } else if (err.response.status == 401) {
      //Message.error({message: err.response.data.message,showClose: true});
      Message.error({message: '尚未登录,请登录!',showClose: true});
      router.replace('/');
    }else if (err.response.status == 500) {
      Message.error({message:err.response.data.message,showClose: true});
    }else if (err.response == '') {
      this.$alert(err.response.message, {
        confirmButtonText: '确定',
        iconClass:'fa fa-exclamation-triangle',
        title:'EXCEPTION!服务端存在异常!'
      });
      router.replace('/');
    }else {
      if (err.response.data.message) {
        Message.error({message: err.response.data.message,showClose: true});
      }else{
        Message.error({message: '未知错误!',showClose: true});
      }
    }
  }
)


let base = '';
export const postKeyValueRequest = (url, params)=> {
  return axios({
    method: 'POST',
    url: `${base}${url}`,
    data: params,
    transformRequest: [function (data) {
      let ret = ''
      for (let it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret;
    }],
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  });
}


export const postRequest = (url, params)=>{
  return axios({
    method: 'POST',
    url: `${base}${url}`,
    data: params,
    transformRequest: [function (data) {
      let ret = ''
      for (let it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret
    }],
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  });
}

export const putRequest = (url, params) => {
  return axios({
    method: 'PUT',
    url: `${base}${url}`,
    data: params,
    transformRequest: [function (data) {
      let ret = ''
      for (let it in data) {
        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
      }
      return ret
    }],
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  });
}

export const getRequest = (url) => {
  return axios({
    method: 'GET',
    url: `${base}${url}`
  });
}

export const deleteRequest = (url) => {
  return axios({
    method: 'DELETE',
    url: `${base}${url}`
  });
}

export const uploadFileRequest = (url,params) => {
  return axios({
    method: 'POST',
    url: `${base}${url}`,
    data: params,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
}

export const downloadRequest = (url, params)=>{
  return axios({
    method: 'GET',
    url: `${base}${url}`,
    responseType: 'blob',
    headers: {
      'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8'
    }
  });
}